//
//  UIBarButtonItem+MLKit.m
//  Pods
//
//  Created by molon on 15/8/3.
//
//

#import "UIBarButtonItem+MLKit.h"

@implementation UIBarButtonItem (MLKit)

+ (instancetype)barButtonItemForTitle:(NSString*)title font:(UIFont*)font color:(UIColor*)color target:(id)target action:(SEL)action
{
    return [self barButtonItemForTitle:title font:font color:color target:target action:action offset:UIOffsetZero];
}

+ (instancetype)barButtonItemForTitle:(NSString*)title font:(UIFont*)font color:(UIColor*)color target:(id)target action:(SEL)action offset:(UIOffset)offset
{
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc]initWithTitle:title style:UIBarButtonItemStylePlain target:target action:action];
    
    NSMutableDictionary *attributesForBarItem = [NSMutableDictionary dictionary];
    if (font) {
        attributesForBarItem[NSFontAttributeName] = font;
    }
    if (color) {
        attributesForBarItem[NSForegroundColorAttributeName] = color;
    }
    NSShadow *shadow = [NSShadow new];
    shadow.shadowColor = [UIColor clearColor];
    attributesForBarItem[NSShadowAttributeName] = shadow;
    [barButtonItem setTitleTextAttributes:attributesForBarItem forState:UIControlStateNormal];
    
    [barButtonItem setTitlePositionAdjustment:offset forBarMetrics:UIBarMetricsDefault];
    
    return barButtonItem;
}

+ (instancetype)barButtonItemWithImage:(UIImage*)image target:(id)target action:(SEL)action
{
    UIBarButtonItem *barButtonItem = nil;
    
    if ([image respondsToSelector:@selector(imageWithRenderingMode:)]) {
        UIImage *finalImage = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        barButtonItem = [[UIBarButtonItem alloc]initWithImage:finalImage landscapeImagePhone:finalImage style:UIBarButtonItemStylePlain target:target action:action];
    }else{
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 0, 20, 20);
        [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
        [button setImage:image forState:UIControlStateNormal];
        
        barButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    }
    
    return barButtonItem;
}


@end
